const path = require('path')
const utils = require('./build/utils')

// 用于做相应的merge处理
const merge = require('webpack-merge')
const {DefinePlugin} = require('webpack')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const isPro = process.env.NODE_ENV === 'production'
const resolve = dir => {
  return path.join(__dirname, dir)
}

let baseUrl = '';
module.exports = {
  baseUrl: baseUrl,
  outputDir: 'dist',
  productionSourceMap: true,
  pages: utils.setPages({}), // 多页面配置入口和出口
  css: {
    loaderOptions: {
        postcss: {
          plugins: [
            require('postcss-px2rem')({remUnit: 30}), // 换算的基数
            require('autoprefixer')({ browsers: ['defaults', 'ios >= 6.0']}), // 补全前缀
        ]
        }
    }
  },
  chainWebpack: config => {
    config.module
    .rule('images')
    .use('url-loader')
    .tap(options =>
      merge(options, {
        limit: 5120,
      })
    )
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
    // 配置目录缩写
    config.resolve.alias
    .set('@', resolve('src'))
    .set('_common', resolve('src/common'))
    .set('_css', resolve('src/css'))
    .set('_components', resolve('src/components'))
    .set('_img', resolve('src/images'))
    .set('_services', resolve('src/services'))
    .set('_images', resolve('src/images'))
  },
  configureWebpack: config => {
    if (isPro) {
      return {
        plugins: [
          // 开发环境开启 Gzip 压缩
          new CompressionWebpackPlugin({
            asset: '[path].gz[query]',
            algorithm: 'gzip',
            test: new RegExp(
              '\\.(js|css)$'
            ),
            threshold: 10240,
            minRatio: 0.8
          }),
          // 使用包分析工具
          // new BundleAnalyzerPlugin()
        ]
      }
    }
  },

  devServer: {
    historyApiFallback: {
      rewrites: [
        {from: new RegExp(baseUrl + 'home'), to: baseUrl + 'home.html'},
        {from: new RegExp(baseUrl + 'personalCenter'), to: baseUrl + 'personalCenter.html'},
        {from: new RegExp(baseUrl + 'memberOrder'), to: baseUrl + 'memberOrder.html'},
        {from: new RegExp(baseUrl + 'goodsDetail'), to: baseUrl + 'goodsDetail.html'},
        {from: new RegExp(baseUrl + 'message'), to: baseUrl + 'message.html'},
        {from: new RegExp(baseUrl + 'bankCard'), to: baseUrl + 'bankCard.html'},
        {from: new RegExp(baseUrl + 'login'), to: baseUrl + 'login.html'},
        {from: new RegExp(baseUrl + 'rewardRecord'), to: baseUrl + 'rewardRecord.html'},
        {from: new RegExp(baseUrl + 'userRelation'), to: baseUrl + 'userRelation.html'},
        {from: new RegExp(baseUrl + 'address'), to: baseUrl + 'address.html'},
        {from: new RegExp(baseUrl + 'add'), to: baseUrl + 'add.html'},
        {from: new RegExp(baseUrl + 'register'), to: baseUrl + 'register.html'},
        {from: new RegExp(baseUrl + 'memberCard'), to: baseUrl + 'memberCard.html'},
        {from: new RegExp(baseUrl + 'comment'), to: baseUrl + 'comment.html'},
        {from: new RegExp(baseUrl + 'complain'), to: baseUrl + 'complain.html'},
        {from: new RegExp(baseUrl + 'myComments'), to: baseUrl + 'myComments.html'},
        {from: new RegExp(baseUrl + 'myComplains'), to: baseUrl + 'myComplains.html'},
        {from: new RegExp(baseUrl + 'orderDetail'), to: baseUrl + 'orderDetail.html'},
        {from: new RegExp(baseUrl + 'myOrderdetail'), to: baseUrl + 'myOrderdetail.html'},
        {from: new RegExp(baseUrl + 'myOrder'), to: baseUrl + 'myOrder.html'},
        {from: new RegExp(baseUrl + 'registerSucceed'), to: baseUrl + 'registerSucceed.html'},
        {from: new RegExp(baseUrl + 'password'), to: baseUrl + 'password.html'},
        {from: new RegExp(baseUrl + 'registerNew'), to: baseUrl + 'registerNew.html'}
      ]
    },
    disableHostCheck: true,
    open: true, // 是否自动打开浏览器页面
    host: '127.0.0.1', // 指定使用一个 host。默认是 localhost
    port: 8080, // 端口地址
    https: false, // 使用https提供服务
    // string | Object 代理设置
    proxy: null,
    progress: true,
    // 提供在服务器内部的其他中间件之前执行自定义中间件的能力
    before: app => {
      // `app` 是一个 express 实例
    }
  }
}